//
//  WYCForgetController.m
//  51信用卡
//
//  Created by 中企互联 on 2017/11/3.
//  Copyright © 2017年 中企互联. All rights reserved.
//

#import "WYCForgetController.h"
#import "UIButton+CountDown.h"
#import "RSAUtil.h"
#import "WYCLoginController.h"

@interface WYCForgetController ()
/** 账号输入框*/
@property(nonatomic,strong)UITextField * UserTextField;

/** 密码*/
@property(nonatomic,strong)UITextField * passtexfield;


/** 再次输入密码*/
@property(nonatomic,strong)UITextField * Zpasstexfield;

/** 验证码*/
@property(nonatomic,strong)UITextField * codeTexField;


/** 邀请码*/
@property(nonatomic,strong)UITextField * Rgscodefield;

/** 密码隐藏或显示*/
@property(nonatomic,strong)UIButton * ZpasstexfieldButton;

/** 密码隐藏或显示*/
@property(nonatomic,strong)UIButton * passtexfieldButton;
@end

@implementation WYCForgetController

- (void)viewDidLoad {
    [super viewDidLoad];
    //创建界面
    [self setupCreat];
}

#pragma make ------>>界面创建
- (void)setupCreat{
    
    self.view.backgroundColor = wycwhiteColor;
    
    
    
    UIView * registerView = [[UIView alloc] init];
    registerView.frame = CGRectMake(0, 0, ScreenWidth, 64);
    registerView.backgroundColor = WYCMain;
    [self.view addSubview:registerView];
    
    
    //不是Nav。所以自己写的View和label
    UILabel * NavRigsterLabel = [[UILabel alloc] init];
    NavRigsterLabel.frame = CGRectMake(ScreenWidth / 2 - 200 * px, 30, 400 * px, 68 * px);
    NavRigsterLabel.textColor = [UIColor whiteColor];
    //    NavRigsterLabel.font = [UIFont systemFontOfSize:BigFont];
    NavRigsterLabel.font = [UIFont boldSystemFontOfSize:BigFont];
    NavRigsterLabel.textAlignment = NSTextAlignmentCenter;
    NavRigsterLabel.text = @"忘记密码";
    [registerView addSubview:NavRigsterLabel];
    
    
    
    //返回按钮
    UIButton *  backbutton = [[UIButton alloc] init];
    [backbutton setImage:[UIImage imageNamed:@"Nav_back"] forState:UIControlStateNormal];
    [backbutton addTarget:self action:@selector(backClick) forControlEvents:UIControlEventTouchUpInside];
    backbutton.frame = CGRectMake(30 * px, NavRigsterLabel.y, 66 * px , 66 * px);
    [registerView addSubview:backbutton];
    
    
    ////////////////
    //账号输入框/////
    ///////////////
    UIView *iponeView = [[UIView alloc] init];
    iponeView.frame = CGRectMake(80 * px, CGRectGetMaxY(registerView.frame) + 80 * px, ScreenWidth - 160 * px, 150 * px);
    iponeView.layer.cornerRadius = 75 * px;
    iponeView.layer.borderWidth = 0.5;
    iponeView.layer.borderColor = [UIColor grayColor].CGColor;
    [self.view addSubview:iponeView];
    
    
    //手机号
    UIImageView * iphonelabel = [[UIImageView alloc] init];
    iphonelabel.frame = CGRectMake(40 * px, 25 * px, 100 * px, 100 * px);
    iphonelabel.image = [UIImage imageNamed:@"手机-icon"];
    [iponeView addSubview:iphonelabel];
    
    
    UIView *iponeline = [[UIView alloc] init];
    iponeline.frame = CGRectMake(CGRectGetMaxX(iphonelabel.frame) + 20 * px, iphonelabel.y, 1, iphonelabel.height);
    iponeline.backgroundColor = [UIColor grayColor];
    [iponeView addSubview:iponeline];
    
    UITextField * userTextField = [[UITextField alloc] init];
    userTextField.frame = CGRectMake(CGRectGetMaxX(iponeline.frame) + 10 * px, iphonelabel.y, iponeView.width - iphonelabel.width + 10 * px, iphonelabel.height);
    //    userTextField.backgroundColor = [UIColor whiteColor];
    userTextField.placeholder = @"请输入手机号";
    userTextField.keyboardType = UIKeyboardTypeNumberPad;
    //    userTextField.text = @"13578728678";
    [iponeView addSubview:userTextField];
    self.UserTextField = userTextField;
    
    UIButton * usertexfieldButton = [[UIButton alloc] init];
    usertexfieldButton.frame = CGRectMake(userTextField.width - 40 * px, userTextField.y, 80 * px, 80 * px);
    [usertexfieldButton addTarget:self action:@selector(usertexfieldButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [usertexfieldButton setImage:[UIImage imageNamed:@"celar"] forState:UIControlStateNormal];
    [iponeView addSubview:usertexfieldButton];
    
    
    ////////////////
    /////密码输入框/////
    ///////////////
    UIView *codeView = [[UIView alloc] init];
    codeView.frame = CGRectMake(80 * px, CGRectGetMaxY(iponeView.frame) + 80 * px, ScreenWidth - 160 * px, 150 * px);
    codeView.layer.cornerRadius = 75 * px;
    codeView.layer.borderWidth = 0.5;
    codeView.layer.borderColor = [UIColor grayColor].CGColor;
    [self.view addSubview:codeView];
    
    
    //手机号
    UIImageView * codelabel = [[UIImageView alloc] init];
    codelabel.frame = CGRectMake(40 * px, 25 * px, 100 * px, 100 * px);
    codelabel.image = [UIImage imageNamed:@"yanzhengma_icon"];
    [codeView addSubview:codelabel];
    
    
    UIView *codeline = [[UIView alloc] init];
    codeline.frame = CGRectMake(CGRectGetMaxX(codelabel.frame) + 20 * px, codelabel.y, 1, iphonelabel.height);
    codeline.backgroundColor = [UIColor grayColor];
    [codeView addSubview:codeline];
    
    UITextField * codeField = [[UITextField alloc] init];
    codeField.frame = CGRectMake(CGRectGetMaxX(codeline.frame) + 10 * px, codelabel.y, iponeView.width - iphonelabel.width + 10 * px, iphonelabel.height);
    codeField.placeholder = @"请输入验证码";
    codeField.keyboardType = UIKeyboardTypeNumberPad;
    [codeView addSubview:codeField];
    _codeTexField = codeField;
    
    
    
    
    
    //    //手机提示线
    //    UIView * passlineView = [[UIView alloc] init];
    //    passlineView.frame = CGRectMake(codefield.x, CGRectGetMaxY(codefield.frame) + 5 * px, iponeView.width, 1);
    //    passlineView.backgroundColor = WYCGlobalBg;
    //    [self.view addSubview:passlineView];
    //
    //获取验证码
    UIButton * codebutton = [[UIButton alloc] init];
    codebutton.frame = CGRectMake(ScreenWidth - 510 * px, codeField.y, 300 * px, codeField.height);
    [codebutton setTitleColor:WYCMain forState:UIControlStateNormal];
    codebutton.titleLabel.font = [UIFont systemFontOfSize:SmallFont];
    [codebutton setTitle:@"获取验证码" forState:UIControlStateNormal];
    [codebutton addTarget:self action:@selector(countDownBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [codeView addSubview:codebutton];
    
    
    
    ////////////////
    //密码输入框/////
    ///////////////
    
    
    UIView *passView = [[UIView alloc] init];
    passView.frame = CGRectMake(80 * px, CGRectGetMaxY(codeView.frame) + 80 * px, ScreenWidth - 160 * px, 150 * px);
    passView.layer.cornerRadius = 75 * px;
    passView.layer.borderWidth = 0.5;
    passView.layer.borderColor = [UIColor grayColor].CGColor;
    [self.view addSubview:passView];
    
    
    //手机号
    UIImageView * passlabel = [[UIImageView alloc] init];
    passlabel.frame = CGRectMake(40 * px, 25 * px, 100 * px, 100 * px);
    passlabel.image = [UIImage imageNamed:@"密码-icon"];
    [passView addSubview:passlabel];
    
    
    UIView *passline = [[UIView alloc] init];
    passline.frame = CGRectMake(CGRectGetMaxX(iphonelabel.frame) + 20 * px, iphonelabel.y, 1, iphonelabel.height);
    passline.backgroundColor = [UIColor grayColor];
    [passView addSubview:passline];
    
    UITextField * passTextField = [[UITextField alloc] init];
    passTextField.frame = CGRectMake(CGRectGetMaxX(passline.frame) + 10 * px, passlabel.y, iponeView.width - iphonelabel.width + 10 * px, iphonelabel.height);
    //    userTextField.backgroundColor = [UIColor whiteColor];
    passTextField.placeholder = @"请输入密码";
    //    passTextField.keyboardType = UIKeyboardTypeNumberPad;
    //    userTextField.text = @"13578728678";
    [passView addSubview:passTextField];
    self.passtexfield = passTextField;
    
    
    UIButton * passtexfieldButton = [[UIButton alloc] init];
    passtexfieldButton.frame = CGRectMake(passTextField.width - 40 * px, passTextField.y, 80 * px, 80 * px);
    [passtexfieldButton addTarget:self action:@selector(passtexfieldButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [passtexfieldButton setImage:[UIImage imageNamed:@"隐藏"] forState:UIControlStateNormal];
    [passtexfieldButton setImage:[UIImage imageNamed:@"展示"] forState:UIControlStateSelected];
    [passView addSubview:passtexfieldButton];
    self.passtexfieldButton = passtexfieldButton;
    
    
    
    /************再次输入密码************/
    
    UIView *ZpassView = [[UIView alloc] init];
    ZpassView.frame = CGRectMake(80 * px, CGRectGetMaxY(passView.frame) + 80 * px, ScreenWidth - 160 * px, 150 * px);
    ZpassView.layer.cornerRadius = 75 * px;
    ZpassView.layer.borderWidth = 0.5;
    ZpassView.layer.borderColor = [UIColor grayColor].CGColor;
    [self.view addSubview:ZpassView];
    
    
    //手机号
    UIImageView * Zpasslabel = [[UIImageView alloc] init];
    Zpasslabel.frame = CGRectMake(40 * px, 25 * px, 100 * px, 100 * px);
    Zpasslabel.image = [UIImage imageNamed:@"密码-icon"];
    [ZpassView addSubview:Zpasslabel];
    
    
    UIView *Zpassline = [[UIView alloc] init];
    Zpassline.frame = CGRectMake(CGRectGetMaxX(Zpasslabel.frame) + 20 * px, Zpasslabel.y, 1, iphonelabel.height);
    Zpassline.backgroundColor = [UIColor grayColor];
    [ZpassView addSubview:Zpassline];
    
    UITextField * ZpassTextField = [[UITextField alloc] init];
    ZpassTextField.frame = CGRectMake(CGRectGetMaxX(Zpassline.frame) + 10 * px, Zpasslabel.y, iponeView.width - iphonelabel.width + 10 * px, iphonelabel.height);
    //    userTextField.backgroundColor = [UIColor whiteColor];
    ZpassTextField.placeholder = @"再次输入密码";
    //    ZpassTextField.keyboardType = UIKeyboardTypeNumberPad;
    //    userTextField.text = @"13578728678";
    [ZpassView addSubview:ZpassTextField];
    self.Zpasstexfield = ZpassTextField;
    
    UIButton * ZpasstexfieldButton = [[UIButton alloc] init];
    ZpasstexfieldButton.frame = CGRectMake(ZpassTextField.width - 40 * px, ZpassTextField.y, 80 * px, 80 * px);
    [ZpasstexfieldButton addTarget:self action:@selector(ZpasstexfieldButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [ZpasstexfieldButton setImage:[UIImage imageNamed:@"隐藏"] forState:UIControlStateNormal];
    [ZpasstexfieldButton setImage:[UIImage imageNamed:@"展示"] forState:UIControlStateSelected];
    [ZpassView addSubview:ZpasstexfieldButton];
    self.ZpasstexfieldButton = ZpasstexfieldButton;
    
    
    //注册按钮
    UIButton * loginButton = [[UIButton alloc] init];
    loginButton.frame = CGRectMake(80 * px, CGRectGetMaxY(ZpassView.frame) + 200 * px, ScreenWidth - 160 * px, 150 * px);
    loginButton.backgroundColor = WYCMain;
    loginButton.layer.cornerRadius = 75 * px;
    [loginButton setTitle:@"找回" forState:UIControlStateNormal];
    loginButton.titleLabel.font = [UIFont systemFontOfSize:72 * px];
    [loginButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [loginButton addTarget:self action:@selector(RegisterClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:loginButton];
    
    
}
//键盘回收
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [_UserTextField resignFirstResponder];
    [_codeTexField resignFirstResponder];
    [_passtexfield resignFirstResponder];
    [_Zpasstexfield resignFirstResponder];
}

#pragma make ------>>获取验证码
    - (void)countDownBtnAction:(UIButton *)button
    {
        WYCLog(@"----------%@",self.UserTextField.text);
        WYCLogFunc;
        if (self.UserTextField.text.length == 0) {
            
            NSString * showStr = @"手机号不能为空";
            [SVProgressHUD showImage:[UIImage imageNamed:@""] status:showStr];
            return;
        }else{
            [button startWithTime:59 title:@"点击重新获取" countDownTitle:@"s后重新获取" mainColor:[UIColor colorWithRed:0 / 255.0 green:0 / 255.0 blue:0 / 255.0 alpha:0] countColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:0]];
            NSString * httpsStr = [NSString stringWithFormat:@"%@mobile=%@&type=%@",Rcode,self.UserTextField.text,@"fpwd"];
            
            [HttpTool POST:httpsStr params:nil success:^(NSURLSessionDataTask *task, id responseObject) {
                WYCLog(@"验证码%@",responseObject);
                
                if ([[responseObject objectForKey:@"code"] intValue] == 0) {
                    
                    [SVProgressHUD showImage:[UIImage imageNamed:@""] status:msgStr];
                                    NSDictionary * dict = responseObject[@"result"];
                    
                                    WYCAccount * Account = [[WYCAccount alloc] initWithDic:dict];
                    
                                    [WYCAccountTool saveAccount:Account];
                    
                                    [WYCIsAppLoginTool saveIsAppLogin:YES];
                    
                }else{
                    
                    [SVProgressHUD showImage:[UIImage imageNamed:@""] status:msgStr];
                }
                
            } fail:^(NSURLSessionDataTask *task, NSError *error) {
                
            }];
            
        }
    }
    
#pragma make ------>>注册按钮
    - (void)RegisterClick {
        WYCLogFunc;
        // 手机号判断是否为空
        if(self.UserTextField.text.length == 0)
        {
            [SVProgressHUD showImage:[UIImage imageNamed:@""] status:@"手机号不能为空"];
            return;
        }
        //验证码判断是否为空
        if(self.codeTexField.text.length == 0)
        {
            [SVProgressHUD showImage:[UIImage imageNamed:@""] status:@"请输入验证码"];
            return;
        }
        //手机号判断是否为空
        if(self.passtexfield.text.length == 0)
        {
            [SVProgressHUD showImage:[UIImage imageNamed:@""] status:@"密码不能为空"];
            return;
        }
        //密码判断是否为空
        
        if(self.passtexfield.text != self.Zpasstexfield.text)
        {
            [SVProgressHUD showImage:[UIImage imageNamed:@""] status:@"两次输入密码不一致"];
            return;
        }
       
        NSMutableDictionary * params = [NSMutableDictionary dictionary];
        NSString * passStr = [NSString stringWithFormat:@"%@",self.passtexfield.text];
        NSString *pas = [RSAUtil encryptString:passStr publicKey:RSA_P_KEY];
        //
        
        params[@"ticket"] = [WYCAccountTool unarchiveticket];
        params[@"authcode"] = self.codeTexField.text;
        params[@"newPwd"] = pas;
        
        WYCLog(@"--------%@",params);
        [HttpTool POST:safemodifyPasswordByForget params:params success:^(NSURLSessionDataTask *task, id responseObject) {
            
            WYCLog(@"找回密码%@",responseObject);
            if ([[responseObject objectForKey:@"code"] intValue] == 0) {
                [self showToast:@"找回成功"];
                [self backClick];
            }else{
                [self showToast:msgStr];
            }
            
        } fail:^(NSURLSessionDataTask *task, NSError *error) {
            [self showToast:Failed];
        }];
        
    }
- (void)passtexfieldButtonClick{
        self.passtexfieldButton.selected = !self.passtexfieldButton.selected;
        if (self.passtexfieldButton.selected == YES) {
            self.passtexfield.secureTextEntry = NO;
        }else{
            
            self.passtexfield.secureTextEntry = YES;
        }
        
    }
    
- (void)ZpasstexfieldButtonClick{
        self.ZpasstexfieldButton.selected = !self.ZpasstexfieldButton.selected;
        if (self.Zpasstexfield.selected == YES) {
            self.Zpasstexfield.secureTextEntry = NO;
        }else{
            
            self.Zpasstexfield.secureTextEntry = YES;
        }
        
    }
- (void)usertexfieldButtonClick{
        
        self.UserTextField.text = @"";
}
    
    
    
#pragma make ------>>返回按钮
- (void)backClick{
        [self dismissViewControllerAnimated:NO completion:nil];
}
@end
